﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace Presentation.Infrastructure
{
    public class NavigationService
    {
        #region Singleton

        private static NavigationService _instance;
        public static NavigationService Instance
        {
            get { return _instance ?? (_instance = new NavigationService()); }
        }

        #endregion

        private Grid _targetRegion;
        private FrameworkElement _currentView;
        private LoginView _loginView;
        private DashboardView _dashboardView;
        private ReportPanneView _reportPanneView;

        private NavigationService()
        {
        }

        public void AttachTargetRegion(Grid targetRegion)
        {
            _targetRegion = targetRegion;
        }

        public void NavigateTo(Type viewType, bool refresh = false)
        {
            FrameworkElement view = null;

            if (viewType.IsAssignableFrom(typeof(LoginView)))
            {
                if(_loginView == null)
                {
                    _loginView = new LoginView {DataContext = new LoginViewModel()};
                    _targetRegion.Children.Add(_loginView);
                }
                Transition(ref _currentView, _loginView);
            }

            if (viewType.IsAssignableFrom(typeof(DashboardView)))
            {
                if (_dashboardView == null)
                {
                    _dashboardView = new DashboardView { DataContext = new DashboardViewModel() };
                    _targetRegion.Children.Add(_dashboardView);
                }
                else
                {
                    if (refresh)
                    {
                        ((DashboardViewModel)_dashboardView.DataContext).RefreshSignalements();
                    }
                }
                Transition(ref _currentView, _dashboardView);
            }

            if (viewType.IsAssignableFrom(typeof(ReportPanneView)))
            {
                if (_reportPanneView == null)
                {
                    _reportPanneView = new ReportPanneView() { DataContext = new ReportPanneViewModel() };
                    _targetRegion.Children.Add(_reportPanneView);
                }
                Transition(ref _currentView, _reportPanneView);
            }
        }

        private void Transition(ref FrameworkElement currentView, FrameworkElement newView)
        {
            const double animationDuration = 400;

            if (currentView == null)
            {
                newView.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(animationDuration))));
            }
            else
            {
                currentView.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation(1, 0, new Duration(TimeSpan.FromMilliseconds(animationDuration)), FillBehavior.Stop));
                newView.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(animationDuration))));
                Panel.SetZIndex(currentView, 0);
            }

            Panel.SetZIndex(newView, 10);

            currentView = newView;
        }
    }
}
